/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseUserPermissionDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseUserPermissionDao : BaseDao, IBaseDao
    {
        public BaseUserPermissionDao()
        {
            base.CurrentTableName = BaseResourcePermissionTable.TableName;
        }

        public BaseUserPermissionDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseUserPermissionDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseUserPermissionDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string[] GetPermissionIDs(string userID)
        {
            string[] names = new string[2];
            string[] values = new string[2];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            values[0] = "User";
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            values[1] = userID;
            return BaseBusinessLogic.FieldToArray(this.GetDT(names, values), BaseResourcePermissionTable.FieldPermissionID);
        }

        public int Grant(string userID, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, permissionIDs.Length);
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                this.Grant(resourcePermissionDao, strArray[i], userID, permissionIDs[i]);
                num++;
            }
            return num;
        }

        public string Grant(string userID, string permissionID)
        {
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            return this.Grant(resourcePermissionDao, string.Empty, userID, permissionID);
        }

        public int Grant(string[] userIDs, string permissionID)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, userIDs.Length);
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < userIDs.Length; i++)
            {
                this.Grant(resourcePermissionDao, strArray[i], userIDs[i], permissionID);
                num++;
            }
            return num;
        }

        public int Grant(string[] userIDs, string[] permissionIDs)
        {
            int num = 0;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseResourcePermissionTable.TableName, userIDs.Length * permissionIDs.Length);
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < userIDs.Length; i++)
            {
                for (int j = 0; j < permissionIDs.Length; j++)
                {
                    this.Grant(resourcePermissionDao, strArray[(i * permissionIDs.Length) + j], userIDs[i], permissionIDs[j]);
                    num++;
                }
            }
            return num;
        }

        private string Grant(BaseResourcePermissionDao resourcePermissionDao, string pramID, string userID, string permissionID)
        {
            BaseResourcePermissionEntity resourcePermissionEntity = new BaseResourcePermissionEntity
            {
                ID = pramID,
                ResourceCategory = "User",
                ResourceID = userID,
                PermissionID = permissionID
            };
            return resourcePermissionDao.Add(resourcePermissionEntity);
        }

        public int Revoke(string[] userIDs, string[] permissionIDs)
        {
            int num = 0;
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < userIDs.Length; i++)
            {
                for (int j = 0; j < permissionIDs.Length; j++)
                {
                    num += this.Revoke(resourcePermissionDao, userIDs[i], permissionIDs[j]);
                }
            }
            return num;
        }

        public int Revoke(string userID, string permissionID)
        {
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            return this.Revoke(resourcePermissionDao, userID, permissionID);
        }

        public int Revoke(string userID, string[] permissionIDs)
        {
            int num = 0;
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < permissionIDs.Length; i++)
            {
                num += this.Revoke(resourcePermissionDao, userID, permissionIDs[i]);
            }
            return num;
        }

        public int Revoke(string[] userIDs, string permissionID)
        {
            int num = 0;
            BaseResourcePermissionDao resourcePermissionDao = new BaseResourcePermissionDao(base.DbHelper, base.UserInfo);
            for (int i = 0; i < userIDs.Length; i++)
            {
                num += this.Revoke(resourcePermissionDao, userIDs[i], permissionID);
            }
            return num;
        }

        private int Revoke(BaseResourcePermissionDao resourcePermissionDao, string userID, string permissionID)
        {
            string[] names = new string[3];
            string[] strArray2 = new string[3];
            names[0] = BaseResourcePermissionTable.FieldResourceCategory;
            strArray2[0] = "User";
            names[1] = BaseResourcePermissionTable.FieldResourceID;
            strArray2[1] = userID;
            names[2] = BaseResourcePermissionTable.FieldPermissionID;
            strArray2[2] = permissionID;
            return resourcePermissionDao.BatchDelete(names, (object[])strArray2);
        }
    }
}